module ProjectHelper
  def project_label(code)
    label_array = Array.new
    code_array = code.split('|')
    code_array.each_with_index do |c,i|
      if i+1==code_array.size
        project = Project.find(:first, :conditions=>["id=?", c], :order=>"id")
        if project
          label_array = label_array << project.name
        else
          label_array = label_array << "..."
        end
      else
        label_array = label_array << "..."
      end
    end
    return label_array.join("/")
  end

  def construct_dtree(all_projects)
    begin
      word_array = Array.new
      word_array = word_array << "d = new dTree('d');"
      word_array = word_array << "d.add(0,-1,'');"
      all_projects.each do |p|
        project_id = get_id_from_code(p)
        if session[:selected_project_id]
          word_array = word_array << "d.add(#{project_id},#{get_parent_id_from_code(p)},'#{get_name_from_code(p)}','/project/select_project/#{get_gencode_from_code(p)}','','','','',#{is_parent_of_selected_project(project_id, session[:selected_project_id].to_i)});"
        else
          word_array = word_array << "d.add(#{project_id},#{get_parent_id_from_code(p)},'#{get_name_from_code(p)}','/project/select_project/#{get_gencode_from_code(p)}','','','','');"
        end
      end
      word_array = word_array << "d.config.useCookies = false;"
      if session[:selected_project_id]
        word_array = word_array << "d.selectedNode = #{session[:selected_project_id]};"
      end
      word_array = word_array << "document.write(d);"
      return word_array.join('')
    rescue
      return ''
    end
  end

  def construct_dtree2(all_projects)
    begin
      word_array = Array.new
      word_array = word_array << "d2 = new dTree('d2');"
      word_array = word_array << "d2.add(0,-1,'');"
      all_projects.each do |p|
        project_id = get_id_from_code(p)
        if session[:selected_project_id]
          word_array = word_array << "d2.add(#{project_id},#{get_parent_id_from_code(p)},'#{get_name_from_code(p)}','/project/select_project/#{get_gencode_from_code(p)}','','','','',#{is_parent_of_selected_project(project_id, session[:selected_project_id].to_i)});"
        else
          word_array = word_array << "d2.add(#{project_id},#{get_parent_id_from_code(p)},'#{get_name_from_code(p)}','/project/select_project/#{get_gencode_from_code(p)}','','','','');"
        end
      end
      word_array = word_array << "d2.config.useCookies = false;"
      if session[:selected_project_id]
        word_array = word_array << "d2.selectedNode = #{session[:selected_project_id]};"
      end
      word_array = word_array << "document.write(d2);"
      return word_array.join('')
    rescue
      return ''
    end
  end

  def get_id_from_code(code)
    code_array = code.split('|')
    return code_array.last.to_i
  end

  def get_name_from_code(code)
    code_array = code.split('|')
    project = Project.find(:first, :conditions=>["id=?", code_array.last.to_i])
    if project
      return project.name
    else
      return ''
    end
  end

  def get_gencode_from_code(code)
    code_array = code.split('|')
    project = Project.find(:first, :conditions=>["id=?", code_array.last.to_i])
    if project
      return project.gencode
    else
      return ''
    end
  end

  def get_parent_id_from_code(code)
    code_array = code.split('|')
    if code_array.size==1
      return 0
    else
      return code_array[code_array.size-2]
    end
  end

  def get_open_from_code(code)
    code_array = code.split('|')
    if code_array.include? session[:selected_project_id].to_s
      return true
    else
      return false
    end
  end

  def is_parent_of_selected_project(project_id, current_id)
    is_parent = false
    current_project = Project.find(:first ,:conditions=>["id=?", current_id])
    if current_project.parent_id
      if project_id==current_project.parent_id
        is_parent = true
      else
        is_parent = is_parent_of_selected_project(project_id, current_project.parent_id)
      end
    end
    return is_parent
  end
end
